/**
 * Created by zhgk on 2016/9/19 0019.
 */
import {Component, OnInit} from "@angular/core";
import {Router} from "@angular/router";
import {baseURL} from "../common";
import {HttpService} from "../common/httpService";
import any = jasmine.any;
import {ToasterService} from "angular2-toaster";

@Component({
    selector: "main-component",
    template: require("./main.component.html"),
    styles: [require("./main.component.css")],
})

export class MainComponent implements OnInit {

    constructor(private router: Router, private hs: HttpService, private ts: ToasterService) {

    }

    ngOnInit(): void {
        this.queryTopMenu()
    }

    //定义顶级目录菜单
    topMenuVos: any[] = [];
    topActiveId: string = "";
    leftMenuVos: any[] = [];
    leftActiveId: string = "";
    //查询顶级目录
    queryTopMenu() {
        let topUrl = baseURL + "/bm/sm/menu/child/0";
        this.hs.query(topUrl).subscribe(resData=> {
            this.topMenuVos = resData.data;
            if (this.topMenuVos.length > 0) {
                this.topActiveId = this.topMenuVos[0].ID;
                this.queryLeftMenu(this.topActiveId);
            }
        });
    }

    //查询根据顶部菜单 ，查询左侧菜单
    queryLeftMenu(pid: string) {
        let topUrl = baseURL + "/bm/sm/menu/child/" + pid;
        this.hs.query(topUrl).subscribe(resData=> {
            this.leftMenuVos = resData.data;
            if (this.leftMenuVos.length > 0) {
                this.leftActiveId = this.leftMenuVos[0].ID;
                this.tabName = this.leftMenuVos[0].NAME;
                let path = this.leftMenuVos[0].PATH;
                if (path) {
                    this.router.navigate([path + ""], {skipLocationChange: true});
                }

            }
        })
    }


    menuClick(menu: any): void {
        this.topActiveId = menu.ID;
        this.queryLeftMenu(menu.ID);
    }

    tabName: string = "";

    leftMenuClick(menu: any): void {
        if (menu.PATH) {
            this.leftActiveId = menu.ID;
            this.tabName = menu.NAME;
            this.router.navigate([menu.PATH],{skipLocationChange: true})
        } else {
            this.ts.pop("success","消息通知",menu.NAME+"建设中，静请期待！")
        }

    }

    //登出系统
    doLoginOut(): void {
        this.router.navigate(["/login"])
    }

}